package com.consumer.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.extra.pinyin.PinyinUtil;
import cn.hutool.extra.pinyin.engine.pinyin4j.Pinyin4jEngine;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.consumer.common.MinioUtil;
import com.consumer.dao.DevopsBucketDao;
import com.consumer.entity.DevopsBucket;
import com.consumer.entity.DevopsBucketFile;
import com.consumer.entity.DirectoryVo;
import com.consumer.service.DevopsBucketFileService;
import com.consumer.service.DevopsBucketService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * (DevopsBucket)表服务实现类
 *
 * @author makejava
 * @since 2024-04-28 15:12:00
 */
@Service("devopsBucketService")
@Transactional(rollbackFor = Exception.class)
public class DevopsBucketServiceImpl extends ServiceImpl<DevopsBucketDao, DevopsBucket> implements DevopsBucketService {

    @Resource
    DevopsBucketFileService bucketFileService;
    @Resource
    MinioUtil minioUtil;

    @Override
    public void createDir(DirectoryVo directoryVo) {
        String dirPid = directoryVo.getDirPid();
        String bucketId = directoryVo.getBucketId();
        String dirName = directoryVo.getDirName();
        String dirRemark = directoryVo.getDirRemark();
        String pinyin = PinyinUtil.getPinyin(dirName).replaceAll(" ", "");
        String bucketFileUrl = !StringUtils.isEmpty(dirPid) ? bucketFileService.getById(dirPid).getBucketFileUrl().concat(dirName).concat("/") : "/".concat(bucketId).concat("/").concat(dirName).concat("/");
        DevopsBucketFile bucketFile = DevopsBucketFile.builder()
                .bucketFileId(!StringUtils.isBlank(directoryVo.getBucketFileId()) ? directoryVo.getBucketFileId() : UUID.randomUUID().toString())
                .bucketId(bucketId)
                .bucketFileType("d")
                .bucketFileName(dirName)
                .bucketFileCode(pinyin)
                .bucketFilePid(dirPid)
                .bucketFileRemark(dirRemark)
                .bucketFileUrl(bucketFileUrl)
                .source("5")
                .createUserName("devops")
                .createUser("devops")
                .updateUser("devops")
                .updateUserName("devops")
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        //创建目录第一步: 本地创建目录
        bucketFile.setIsValid("Y");
        boolean save = bucketFileService.save(bucketFile);
//        if (save) {
        //创建目录第二步: minio创建目录
        bucketFileUrl = bucketFileUrl.replaceAll("/".concat(bucketId), "");
        Boolean dir = minioUtil.createDir("devops-" + bucketId, bucketFileUrl);
        //minio 添加失败,手动强制回滚事务，这里一定要第一时间处理 注意⚠️：数据库与minio需要事物同步
//            if (!dir) TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }
    }


    public void saveBucket(DevopsBucket devopsBucket) {
        this.save(devopsBucket);
    }
}

